package com.ruoyi.project.poem.goodsposter.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.poem.goodsposter.domain.GoodsPoster;
import com.ruoyi.project.poem.goodsposter.service.IGoodsPosterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 今日好物Controller
 *
 * @author shixiangwen
 * @date 2021-06-24
 */
@Controller
@RequestMapping("/poem/goodsposter")
public class GoodsPosterController extends BaseController {

    private String prefix = "poem/goodsposter";

    @Autowired
    private IGoodsPosterService goodsPosterService;

    @RequiresPermissions("poem:goodsposter:view")
    @GetMapping()
    public String goodsposter() {
        return prefix + "/goodsposter";
    }

    /**
     * 查询今日好物列表
     */
    @RequiresPermissions("poem:goodsposter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsPoster goodsPoster) {
        startPage();
        List<GoodsPoster> list = goodsPosterService.selectGoodsPosterList(goodsPoster);
        return getDataTable(list);
    }

    /**
     * 导出今日好物列表
     */
    @RequiresPermissions("poem:goodsposter:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsPoster goodsPoster) {
        List<GoodsPoster> list = goodsPosterService.selectGoodsPosterList(goodsPoster);
        ExcelUtil<GoodsPoster> util = new ExcelUtil<GoodsPoster>(GoodsPoster.class);
        return util.exportExcel(list, "goodsposter");
    }

    /**
     * 新增今日好物
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存今日好物
     */
    @RequiresPermissions("poem:goodsposter:add")
    @Log(title = "今日好物", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsPoster goodsPoster) {
        return toAjax(goodsPosterService.insertGoodsPoster(goodsPoster));
    }

    /**
     * 修改今日好物
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GoodsPoster goodsPoster = goodsPosterService.selectGoodsPosterById(id);
        mmap.put("goodsPoster", goodsPoster);
        return prefix + "/edit";
    }

    /**
     * 修改保存今日好物
     */
    @RequiresPermissions("poem:goodsposter:edit")
    @Log(title = "今日好物", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsPoster goodsPoster) {
        return toAjax(goodsPosterService.updateGoodsPoster(goodsPoster));
    }

    /**
     * 删除今日好物
     */
    @RequiresPermissions("poem:goodsposter:remove")
    @Log(title = "今日好物", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsPosterService.deleteGoodsPosterByIds(ids));
    }

    @RequiresPermissions("poem:goodsposter:edit")
    @Log(title = "今日好物", businessType = BusinessType.GRANT)
    @PostMapping("/updateStatus/{id}")
    @ResponseBody
    public AjaxResult updateStatus(@PathVariable("id") Long id, Integer status) {

        GoodsPoster goodsPoster = goodsPosterService.selectGoodsPosterById(id);
        goodsPoster.setStatus(status);

        return toAjax(goodsPosterService.updateGoodsPoster(goodsPoster));
    }
}
